//**********************************************
//*             Global variables               *
//**********************************************
//var ctx;
//var canvas;
var gameLoop;
var IsGameOver = 0;
var scope_width = 1000;
var scope_height = 500;
var stage;
var map;
	
//***********************************************
//*            Game Initialization              *
//***********************************************
function drawGameCanvas()
{
	stage = new JW.Canvas.Stage({
		width    : 1000,
		height   : 500,
		bodyCls  : MyBody,
		renderTo : "body"
	});
	map = stage.body.map;
	
    // Play the game until the ball stops.
    gameLoop = setInterval(moveUnits, 25);
	
	// Add keyboard listener.
	window.addEventListener('keydown', onKeyDown, true);
	window.addEventListener('keyup',   onKeyUp,   true);
	// Add mouse click listener.
	//stage.el.click(mouseDown);
	// Add mouse move listener.
	//stage.el.mousemove(mouseMove);
}
//******************************************
//*              Main Function             *
//******************************************
function moveUnits()
{
	map.hero.move();
	if (IsGameOver == 1){
	    clearInterval(gameLoop);
        alert("Game over!");
 	}
}
function handleMoveKey(keyCode, value)
{
	switch (keyCode){
		case 37:
			// TODO: Left Arrow Action
			map.hero.goL = value;
			break;

		case 39:
			// TODO: Right Arrow Action
			map.hero.goR = value;
			break;

		case 40:
			//TODO: Down Arrow Action
			map.hero.goD = value;
			break;

		case 38:
			// TODO: Up Arrow Action
			map.hero.goU = value;
			break;
	}
}
function onKeyDown(event)
{
	handleMoveKey(event.keyCode, 1);
}
function onKeyUp(event)
{
	handleMoveKey(event.keyCode, 0);
}
